﻿using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using SimpleDbBrowser.Windows;
using SimpleDbBrowser.ViewModel;

namespace SimpleDbBrowser
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// Initializes a new instance of the MainWindow class.
        /// </summary>
        public MainWindow()
        {
            this.ViewModel = new MainViewModel();
            this.DisplayOptions = new RelayCommand(this.OnDisplayOptions);

            // This is where all MainWindow bindings, etc take place, so do it last.
            InitializeComponent();

            // These subscriptions must occur after initialize component.
            ((INotifyCollectionChanged)this.ViewModel.Attributes).CollectionChanged += (s, a) => HandleListViewChanged(this.lstAttrs);
            ((INotifyCollectionChanged)this.ViewModel.Items).CollectionChanged += (s, a) => HandleListViewChanged(this.lstItems);
            ((INotifyCollectionChanged)this.ViewModel.Domains).CollectionChanged += (s, a) => HandleListViewChanged(this.lstDomains);
        }

        /// <summary>
        /// Gets the main window view model.
        /// </summary>
        public MainViewModel ViewModel { get; private set; }

        /// <summary>
        /// Gets the command that displays the options window.
        /// </summary>
        public ICommand DisplayOptions { get; private set; }

        /// <summary>
        /// Executes the DisplayOptions command.
        /// </summary>
        /// <param name="o">
        /// The execute parameter.  This is not used.
        /// </param>
        private void OnDisplayOptions(object o)
        {
            var v = new OptionsView { Owner = this, DataContext = this.ViewModel.OptionsModel };

            var res = v.ShowDialog();
            if (res == true) // It could also be null, so we specify
            {                
                this.ViewModel.RefreshSimpleDb(((OptionsViewModel)v.DataContext).Configuration);
            }
        }

        /// <summary>
        /// Handle the event where the trace log that we're displaying has been modified.
        /// </summary>
        /// <param name="sender">
        /// The source of the event.
        /// </param>
        /// <param name="e">
        /// The event's arguments.
        /// </param>
        private void HandleTraceLogTextChanged(object sender, TextChangedEventArgs e)
        {
            this.txtLogging.ScrollToEnd();
        }

        /// <summary>
        /// Handle the event where a the list view's contents have changed.
        /// </summary>
        /// <param name="associatedListView">
        /// The listview that's bound to this particular collection.
        /// </param>
        private static void HandleListViewChanged(ListView associatedListView)
        {
            foreach (var col in ((GridView)associatedListView.View).Columns)
            {
                if (double.IsNaN(col.Width)) col.Width = col.ActualWidth;

                col.Width = double.NaN;
            }
        }
    }
}
